$(document).ready(function() {
	var ctx = $("#ctx").val();//获取服务器地址
	var role = $("#role").val();//获取当前登录用户的角色
	//获取广告主
	if(role == "agent"){
		//全部广告主
		$("#advertiser_son_ul").hide();
		if($("#advertiserId").val() == -1 || $("#advertiserId").val() == ""){
 			$("#advertiserul_span").text("全部广告主");
 		}
		if($("#main_advertiserul").html()){
			$("#advertiser_son_ul").append("<li value='-1' style='width:130px;'>全部广告主</li>");
			var url = ctx+"/widget/selector/provide/advertiser?parentId="+$("#sessionagentId").val();
			$.getJSON( url, function(data) {
				var data_1 = data.advertiserList;
				$.each( data_1, function( key, val ) {
					$("#advertiser_son_ul").append('<li class="selectop" value='+ val.value +'>' + val.label + '</li>');
				});
			});
		}
	}else if(role == "advertiser"){
		$("#advertiserul_span").text($("#sessionadvertiserName").val());
		$("#advertiserId").val($("#sessionadvertiserId").val());
		$("#advertiserName").val($("#sessionadvertiserName").val());
	}
	 $("#advertiserul_span").on("click",function(){
	 	if(role == "agent"){
	 		if($('#advertiser_son_ul').height()>210){
	 			$('#advertiser_son_ul').addClass("son_ul_change");
	 		}
	 		$('#advertiser_son_ul').show(); //ul显示
	 	}
	 });
	 $('ul#advertiser_son_ul li').live("click",function(){
	 	$(this).parents('li').find('span').text($(this).text());
		$("#advertiserId").val($(this).attr("value"));
		$("#advertiserName").val($(this).text());
		$('#advertiser_son_ul').hide(); //ul隐藏
		$("#orderul_span").text("全部订单");
		$("#orderId").val(-1);
		$("#orderName").val("全部订单");
		$("#adGroupul_span").text("全部策略");
		$("#adGroupId").val(-1);
		$("#adGroupName").text("全部策略");
	});
	$("#advertiserul_box").mouseleave(function(){
		$('#advertiser_son_ul').hide(); //ul隐藏
		$('#advertiser_son_ul').removeClass("son_ul_change");
	});
	
	//全部订单
	$("#orderul_son_ul").hide();
	$("#orderul_span").on("click",function(){
		getSelectData("orderul_son_ul","全部订单",ctx+"/widget/selector/provide/order?parentId=" + $("#advertiserId").val(),$("#advertiserId").val(),"order");
	});
	$('ul#orderul_son_ul li').live("click",function(){
		$(this).parents('li').find('span').text($(this).text());
		$("#orderId").val($(this).attr("value"));
		$("#orderName").val($(this).text());
		$('#orderul_son_ul').hide(); //ul隐藏
		$("#adGroupul_span").text("全部策略");
		$("#adGroupId").val(-1);
		$("#adGroupName").text("全部策略");
	});
	$("#orderul_box").mouseleave(function(){
	 	$('#orderul_son_ul').hide(); //ul隐藏
	});
	
	//全部策略
	$("#adGroupul_son_ul").hide();
	$("#adGroupul_span").on("click",function(){
		getSelectData("adGroupul_son_ul","全部策略",ctx+"/widget/selector/provide/ad-group?parentId=" + $("#orderId").val(),$("#orderId").val(),"adgroup");
	});
	$('ul#adGroupul_son_ul li').live("click",function(){
		$(this).parents('li').find('span').text($(this).text());
		$("#adGroupId").val($(this).attr("value"));
		$("#adGroupName").val($(this).text());
		$('#adGroupul_son_ul').hide(); //ul隐藏
	});
	$("#adGroupul_box").mouseleave(function(){
 		$('#adGroupul_son_ul').hide(); //ul隐藏
 	});
	
	function getSelectData(target_sonulId,defaultName,url,paramValue,tabId){
	 	$("#"+target_sonulId).empty();
	 	$("#"+target_sonulId).append("<li value='-1'>"+defaultName+"</li>");
	 	if(paramValue != -1 && paramValue != ""){
	 	 	$.getJSON( url, function(data) {
	 	 		var data_1 = data[paramValue];
	 	 		if(tabId != null && tabId == "order"){
	 	 			data_1 = data['orderList'];
	 	 		} else if (tabId != null && tabId == "adgroup"){
	 	 			data_1 = data['adGroupList'];
	 	 		}
	 			$.each( data_1, function( key, val ) {
	 				$("#"+target_sonulId).append('<li value='+ val.value +'>' + val.label + '</li>');
	 			});
	 			$("#"+target_sonulId).show(); //ul显示
				if($("#"+target_sonulId).height()>210){
					$("#"+target_sonulId).addClass("son_ul_change");
		 		}
	 		});
	 	}else{
	 		$("#"+target_sonulId).show();
	 	}
	}
	
	//请选择所属行业
 	if($("#main_industryul").html()){
 		if($("#industryId").val() == 0 || $("#industryId").val() == ""){
 			$("#industryul_span").text("请选择所属行业");
 		}
 		$("#industry_son_ul").hide();
 		var url = ctx+"/widget/selector/provide/industry?parentId=ADVERTISER";
 		getSelectData1("industryul_span","industry_son_ul","industryul_select_box",0,"请选择所属行业",url,"allIndustry",null);
 		$('ul#industry_son_ul li').live("click",function(){
 	 		$(this).parents('li').find('span').text($(this).text());
 			$("#industryId").val($(this).attr("value"));
 			$("#industryName").val($(this).text());
 			$('#industry_son_ul').hide(); //ul隐藏
 		});
 	}

	//请选择维度
	if($("#main_dimensionul").html()){
 		$("#dimension_son_ul").hide();
 		var url = ctx+"/advertiser/main/ad/report/advertiser-report/dimensionlist";
 		getSelectData1("dimensionul_span","dimension_son_ul","dimensionul_box",-1,"请选择维度",url,null,"dimension");
 		$('ul#dimension_son_ul li').live("click",function(){
 	 		$(this).parents('li').find('span').text($(this).text());
 			$("#dimensionId").val($(this).attr("value"));
 			$("#dimensionName").val($(this).text());
 			$('#dimension_son_ul').hide(); //ul隐藏
 		});
 	}
	
 	//请选择广告位
 	if($("#main_zoneListUl").html()){
 		if($("#selectedZoneId").val() == -1 || $("#selectedZoneId").val() == ""){
 			$("#zoneListUl_span").text("全部广告位");
 		}
 		$("#son_zoneListUl").hide();
 		var url = "";
 		if($(".zoneList_back").html()){
 			url = ctx+"/widget/selector/provide/zone?parentId="+$("#selectedOperatorId").val();
 		}else{
 			url = ctx+"/widget/selector/provide/zone?parentId="+$("#sessionoperatorId").val();
 		}
 		getSelectData1("zoneListUl_span","son_zoneListUl","zoneListUl_box",-1,"全部广告位",url,"zoneList",null);
 		$('ul#son_zoneListUl li').live("click",function(){
 	 		$(this).parents('li').find('span').text($(this).text());
 			$("#selectedZoneId").val($(this).attr("value"));
 			$("#selectedZoneName").val($(this).text());
 			$('#son_zoneListUl').hide(); //ul隐藏
 			$("#myform").submit();
 		});
 	}
 	
 	//请选择媒体
 	if($("#main_operatorListUl").html()){
 		if($("#selectedOperatorId").val() == -1 || $("#selectedOperatorId").val() == ""){
 			$("#operatorListUl_span").text("全部媒体");
 		}
 		$("#son_operatorListUl").hide();
 		var url = ctx+"/widget/selector/provide/operator?parentId=1";
 		getSelectData1("operatorListUl_span","son_operatorListUl","operatorListUl_box",-1,"全部媒体",url,"operatorList","operator");
 		$('ul#son_operatorListUl li').live("click",function(){
 	 		$(this).parents('li').find('span').text($(this).text());
 			$("#selectedOperatorId").val($(this).attr("value"));
 			$("#selectedOperatorName").val($(this).text());
 			$("#zoneListUl_span").text("全部广告位");
 			$("#selectedZoneId").val("");
 			$("#selectedZoneName").val("全部广告位");
 			$('#son_operatorListUl').hide(); //ul隐藏
 			$("#myform").submit();
 		});
 	}
 	
 	//请选择消费类型
 	if($("#main_typeul").html()){
 		if($("#typeId").val() == 0 || $("#typeId").val() == ""){
 			$("#typeul_span").html("请选择操作类型");
 		}
 		$("#type_son_ul").hide();
 		$('ul#type_son_ul li').live("click",function(){
 			$(this).parents('li').find('span').html($(this).html());
 			$("#typeId").val($(this).attr("typeValue"));
 			$("#typeName").val($(this).html());
 			$('#type_son_ul').hide(); //ul隐藏
 		});
 		$("#typeul_span").on("click",function(){
 	 		$('#type_son_ul').show(); //ul显示
 	 	});
 		$("#typeul_select_box").mouseleave(function(){
 	 		$('#type_son_ul').hide(); //ul隐藏
 	 	});
 	}
 	
 	//请选择代理商
 	if($("#main_agentListUl").html()){
 		$("#son_agentListUl").hide();
 		var url = ctx+"/widget/selector/provide/adv-agent";
 		getSelectData1("agentListUl_span","son_agentListUl","agentListUl_box",-1,"请选择代理商",url,"advAgentsList",null);
 		$('ul#son_agentListUl li').live("click",function(){
 	 		$(this).parents('li').find('span').html($(this).html());
 			$("#agentId").val($(this).attr("value"));
 			$('#son_agentListUl').hide(); //ul隐藏
 		});
 	}
 	
 	function getSelectData1(target_spanId,target_sonulId,target_boxId,defaultValue,defaultName,url,paramValue,tabId){
 		$("#"+target_sonulId).append("<li  value="+defaultValue+">"+defaultName+"</li>");
 		$.getJSON( url, function(data) {
 			if(tabId != null && tabId == "dimension"){
 				$.each( data, function( key, val ) {
 	 				$("#dimension_son_ul").append('<li value='+ val.id +'>' + val.name + '</li>');
 	 			});
	 		}else if(tabId != null && tabId == "operator"){
	 			var data_1 = data[paramValue];
	 			$.each( data_1, function( key, val ) {
	 				$("#"+target_sonulId).append("<li value="+ val.id +">" + val.companyName + "</li>");
	 			});
	 		}else{
	 			var data_1 = data[paramValue];
	 			$.each( data_1, function( key, val ) {
	 				$("#"+target_sonulId).append('<li value='+ val.value +'>' + val.label + '</li>');
	 			});
	 		}
 		});
 		$("#"+target_spanId).on("click",function(){
 			if($("#"+target_sonulId).height()>210){
 				$("#"+target_sonulId).addClass("son_ul_change");
	 		}
 			$("#"+target_sonulId).show(); //ul显示
 	 	});
 		$("#"+target_boxId).mouseleave(function(){
			$("#"+target_sonulId).hide(); //ul隐藏
			$("#"+target_sonulId).removeClass("son_ul_change");
	 	});
 	}
});